package com.ruoyi.zsyh.controller.applet.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.zsyh.domain.*;
import com.ruoyi.zsyh.service.*;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.*;

/**
 * 服务订单
 */
@Controller
@RequestMapping("/applet/zsyh/serviceOrder")
public class ZsyhServiceOrderAppletController extends BaseController
{

    @Autowired
    private IZsyhServiceTypeService zsyhServiceTypeService;

    @Autowired
    private IZsyhUserAddressService zsyhUserAddressService;

    @Autowired
    private IZsyhServiceOrderService zsyhServiceOrderService;

    @Autowired
    private IZsyhPlatformUserService zsyhPlatformUserService;

    @Autowired
    private IZsyhAgentAreaService zsyhAgentAreaService;



    /**
     * 获取服务分类
     * @param pid
     * @return
     */
    @RequestMapping("/getServiceTypeByPid")
    @ResponseBody
    public AjaxResult getServiceTypeByPid(String pid){
        pid = EmptyUtils.isEmpty(pid)?"0":pid;
        ZsyhServiceType type = new ZsyhServiceType();
        type.setParentId(pid);
        return AjaxResult.success("获取成功", zsyhServiceTypeService.selectZsyhServiceTypeList(type));
    }

    /**
     * 获取服务分类 根据一级id 获取二级三级
     * @param pid
     * @return
     */
    @RequestMapping("/getServiceType23ByPid")
    @ResponseBody
    public AjaxResult getServiceType23ByPid(String pid){
        ZsyhServiceType firstType = zsyhServiceTypeService.selectZsyhServiceTypeById(pid);
        String firstName = firstType.getServiceName();
        ZsyhServiceType type = new ZsyhServiceType();
        type.setParentId(pid);
        List <ZsyhServiceType> list =  zsyhServiceTypeService.selectZsyhServiceTypeList(type);
        for(ZsyhServiceType a:list){
            String id = a.getId();
            String secondName = a.getServiceName();
            ZsyhServiceType type1 = new ZsyhServiceType();
            type1.setParentId(id);
            List <ZsyhServiceType> list1 =  zsyhServiceTypeService.selectZsyhServiceTypeList(type1);
            for (ZsyhServiceType b:list1){
                String thirdName = b.getServiceName();

                String nameAll = firstName + "-" + secondName + "-" + thirdName;
                b.setNameAll(nameAll);
                b.setIsCheck("false");
            }

            a.setTypeList(list1);
        }

        return AjaxResult.success("获取成功", list);
    }

    /**
     * 发布服务需求
     * @return
     * @throws Exception
     */
    @RepeatSubmit
    @RequestMapping("/saveServiceOrder")
    @ResponseBody
    public AjaxResult saveServiceOrder(@RequestBody ZsyhServiceOrder serviceOrder)throws Exception
    {

        ZsyhUserAddress address = zsyhUserAddressService.selectZsyhUserAddressById(serviceOrder.getUserAddressId());
        serviceOrder.setId(IdWorker.getId());
        serviceOrder.setUserName(address.getAddressName());
        serviceOrder.setUserPhone(address.getAddressPhone());
        serviceOrder.setUserAddress(address.getAddressPrivinceName() + address.getAddressCityName() + address.getAddressDistrictName() + " " + address.getAddressDetail());
        serviceOrder.setOrderAreaId(address.getAddressDistrict());
        serviceOrder.setOrderState("0");
        if(StringUtils.isNotEmpty(serviceOrder.getFaultComment())){
            serviceOrder.setFaultComment(serviceOrder.getFaultComment());
        }else{
            serviceOrder.setFaultComment("");
        }
        if(StringUtils.isNotEmpty(serviceOrder.getRepairStoreId()))serviceOrder.setRepairStoreId(serviceOrder.getRepairStoreId());
        if(EmptyUtils.isNotEmpty(serviceOrder.getRepairStoreId())){
            serviceOrder.setOrderType("2");
        }else{
            serviceOrder.setOrderType("1");
        }
        String serviceTypeId = serviceOrder.getServiceTypeId();
        ZsyhServiceType type = zsyhServiceTypeService.selectZsyhServiceTypeById(serviceTypeId);
        serviceOrder.setOrderFixdraw(type.getServiceFixdraw());

        ZsyhAgentArea zsyhAgentArea = new ZsyhAgentArea();
        zsyhAgentArea.setAgentDistrict(address.getAddressDistrict());
        List<ZsyhAgentArea> agentAreaList = zsyhAgentAreaService.selectZsyhAgentAreaList(zsyhAgentArea);
        if(agentAreaList.size() > 0){
            serviceOrder.setAgentId(agentAreaList.get(0).getAgentId());
        }

        zsyhServiceOrderService.insertZsyhServiceOrder(serviceOrder);
        return success("操作成功");
    }


    /**
     * 获取店铺评价列表 分页
     * @return
     */
    @RequestMapping("/getShopEvaluateList")
    @ResponseBody
    public AjaxResult getShopEvaluateList(String id, String pageNum, String pageSize)throws Exception
    {
        ZsyhServiceOrder order = new ZsyhServiceOrder();
        order.setOrderState("4");
        order.setRepairStoreId(id);
        Map map = new HashedMap();
        if(EmptyUtils.isNotEmpty(pageNum) && EmptyUtils.isNotEmpty(pageSize)){
            int num = Integer.valueOf(pageNum);
            int size = Integer.valueOf(pageSize);
            num = (num - 1) * size;
            map.put("beginPos", num);
            map.put("pageSize", size);
        }
        order.setParams(map);
        List<ZsyhServiceOrder> orderList = zsyhServiceOrderService.selectZsyhServiceOrderList(order);
        return success("成功", orderList);
    }


    /**
     * 获取服务订单列表
     * @return
     */
    @RequestMapping("/getServiceOrderListByUserId")
    @ResponseBody
    public AjaxResult getServiceOrderListByUserId(String userId, String orderState, String pageNum, String pageSize)throws Exception
    {
        ZsyhServiceOrder order = new ZsyhServiceOrder();
        if(EmptyUtils.isNotEmpty(orderState)){
            order.setOrderState(orderState);
        }
        order.setUserId(userId);
        Map map = new HashedMap();
        if(EmptyUtils.isNotEmpty(pageNum) && EmptyUtils.isNotEmpty(pageSize)){
            int num = Integer.valueOf(pageNum);
            int size = Integer.valueOf(pageSize);
            num = (num - 1) * size;
            map.put("beginPos", num);
            map.put("pageSize", size);
        }
        map.put("orderState", "未取消");
        order.setParams(map);
        List<ZsyhServiceOrder> orderList = zsyhServiceOrderService.selectZsyhServiceOrderList(order);
        return success("成功", orderList);
    }

    /**
     * 获取服务订单详情，电子保单
     * @return
     */
    @RequestMapping("/getServiceOrderDetailsById")
    @ResponseBody
    public AjaxResult getServiceOrderDetailsById(String orderId)throws Exception
    {
        ZsyhServiceOrder order = zsyhServiceOrderService.selectZsyhServiceOrderById(orderId);
        return success("成功", order);
    }

    /**
     * 提交评价
     * @param orderId
     * @param orderEvaluateScore
     * @param orderEvaluateContent
     * @param orderEvaluateImage
     * @return
     * @throws Exception
     */
    @RepeatSubmit
    @RequestMapping("/saveServiceOrderEvaluate")
    @ResponseBody
    public AjaxResult saveServiceOrderEvaluate(String orderId, String orderEvaluateScore, String orderEvaluateContent, String orderEvaluateImage)throws Exception
    {
        ZsyhServiceOrder order = new ZsyhServiceOrder();
        order.setId(orderId);
        order.setOrderState("4");
        order.setOrderEvaluateScore(orderEvaluateScore);
        order.setOrderEvaluateContent(orderEvaluateContent);
        order.setOrderEvaluateImage(orderEvaluateImage);
        zsyhServiceOrderService.updateZsyhServiceOrder(order);
        return success("成功");
    }

    /**
     * 取消订单
     * @param orderId
     * @return
     * @throws Exception
     */
    @RequestMapping("/cancelServiceOrder")
    @ResponseBody
    public AjaxResult cancelServiceOrder(String orderId)throws Exception
    {
        ZsyhServiceOrder order = new ZsyhServiceOrder();
        order.setId(orderId);
        order.setOrderState("5");
        zsyhServiceOrderService.updateZsyhServiceOrder(order);
        return success("成功");
    }
}
